<?php
namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\LeavingMessageModel;

class LeavingMessageController extends Controller{

    public function __construct()
    {
        $this->middleware('user_login');
    }

    //获取我的留言
    public function my_message(LeavingMessageModel $leavingMessageModel){

        $data = $leavingMessageModel
            ->where('user_id',auth('user_api')->user()->id)
            ->paginate(15);
        return returnMsg(0,'获取成功',$data);

    }

    //发表留言
    public function add_message(Request $request,LeavingMessageModel $leavingMessageModel){

        if(empty($request->input('content'))){
            return returnMsg(400,'内容不为空');
        }

        $leavingMessageModel->content = $request->input('content');
        $leavingMessageModel->user_id = auth('user_api')->user()->id;
        $leavingMessageModel->user_name = auth('user_api')->user()->nickName;
        $leavingMessageModel->head_img = auth('user_api')->user()->head_img;

        $res = $leavingMessageModel->save();
        if(!$res){
            return returnMsg(400,'保存失败');
        }

        return returnMsg(0,'保存成功');

    }

}